package com.wc.blue_bridge_test.base.T69;

import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/12/27 22:16
 *
 * @description Huffuman树
 * https://lx.lanqiao.cn/problem.page?gpid=T69
 */
public class Main {
    public static void main(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            queue.add(sc.nextInt());
        }
        int ans = 0;
        while (queue.size() > 1) {
            int a = queue.poll();
            int b = queue.poll();
            int sum = a + b;
            ans += sum;
            queue.add(sum);
        }
        System.out.println(ans);
    }
}
